import 'dart:math';

import 'package:cream_chat_flutter/globals/ThemeProvider.dart';
import 'package:cream_chat_flutter/pages/HomeTabPage/BuildBanner.dart';
import 'package:cream_chat_flutter/pages/HomeTabPage/BuildMyRelatedList.dart';
import 'package:cream_chat_flutter/pages/HomeTabPage/BuildRecommendUsers.dart';
import 'package:cream_chat_flutter/pages/HomeTabPage/HomeTabPageProvider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:provider/provider.dart';


///大首页的tab首页 不需要继承Page
class HomeTabPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomeTabPageState();
}

class _HomeTabPageState extends State<HomeTabPage>
    with SingleTickerProviderStateMixin {
  EasyRefreshController _controller;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
    print("_HomeTabPageState initState");
    HomeTabPageProvider htp = context.read<HomeTabPageProvider>();
    htp.getRecommendUser(true);
  }



  @override
  void dispose() {
    super.dispose();
    // _scrollController.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print("整个");
    return Consumer2<ThemeProvider, HomeTabPageProvider>(
      builder: (BuildContext context, ThemeProvider tp, HomeTabPageProvider htp,
          Widget child) =>
          Scaffold(
              body: SafeArea(
                  child: EasyRefresh.custom(
                    enableControlFinishRefresh: htp.loading,
                    enableControlFinishLoad: htp.hasMore,
                    controller: _controller,
                    header: ClassicalHeader(
                        refreshText: "下拉刷新",
                        refreshReadyText: "准备刷新",
                        refreshingText: "加载中...",
                        refreshedText: "加载完毕",
                        refreshFailedText: "刷新失败，请稍后尝试",
                        noMoreText: "没有找到内容哦～",
                        infoText: "当前时间 %T"),
                    footer: ClassicalFooter(
                        loadText: "上拉加载",
                        loadReadyText: "准备加载",
                        loadingText: "加载中...",
                        loadedText: "加载完毕",
                        loadFailedText: "加载失败，请稍后尝试",
                        noMoreText: "我也是有底线的～",
                        infoText: "当前时间 %T"),
                    onRefresh: () async {
                      print("onRefresh");
                      htp.getRecommendUser(true);
                    },
                    onLoad: () async {
                      print("onLoad");
                      htp.getRecommendUser(false);
                    },
                    slivers: [
                      _buildTitle(),
                      _buildMyRelated(),
                      _buildBanner(),
                      _buildStickyBar(),
                      _buildList(),
                    ],
                  ))),
    );
  }

  Widget _buildTitle() {
    ThemeProvider tp = context.read<ThemeProvider>();
    return SliverToBoxAdapter(
        child: Padding(
            padding: EdgeInsets.only(bottom: tp.getItemMargin()),
            child: Center(
                child: Text("奶油聊天",
                    style: TextStyle(
                        fontWeight: FontWeight.bold, fontSize: 25)))));
  }

  Widget _buildMyRelated() {
    ThemeProvider tp = context.read<ThemeProvider>();
    return SliverToBoxAdapter(
      child: Padding(
        padding: EdgeInsets.only(
            bottom: tp.getItemMargin(),
            left: tp.getPagePadding(),
            right: tp.getPagePadding()),
        child: BuildMyRelatedList(),
      ),
    );
  }

  Widget _buildBanner() {
    ThemeProvider tp = context.read<ThemeProvider>();
    return SliverToBoxAdapter(
      child: Container(
        height: 150,
        padding: EdgeInsets.only(
            bottom: tp.getItemMargin(),
            left: tp.getPagePadding(),
            right: tp.getPagePadding()),
        child: BuildBanner(),
      ),
    );
  }

  Widget _buildStickyBar() {
    return SliverPersistentHeader(
      pinned: true, //是否固定在顶部
      floating: true,
      delegate: _SliverAppBarDelegate(
          minHeight: 50, //收起的高度
          maxHeight: 50, //展开的最大高度
          child: Container(
            padding: EdgeInsets.only(left: 16),
            color: Colors.pink,
            alignment: Alignment.centerLeft,
            child: Text("浮动", style: TextStyle(fontSize: 18)),
          )),
    );
  }

  Widget _buildList() => BuildRecommendUsers();
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate({
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
  });

  final double minHeight;
  final double maxHeight;
  final Widget child;

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => max(maxHeight, minHeight);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}
